﻿<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2008/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>spchol302_ex2_feature_activated</Title>
      <Shortcut>spchol302_ex2_feature_activated</Shortcut>
      <Description>Code snippet for SPCHOL302 Ex2 Feature Activated</Description>
      <Author>Microsoft Corporation</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="csharp">
        <![CDATA[
using (SPWeb spWeb = properties.Feature.Parent as SPWeb)
            {
                SPContentType newAnnouncement = spWeb
                    .ContentTypes
                    .Cast<SPContentType>()
                    .FirstOrDefault(c => c.Name == "New Announcements");
                if (newAnnouncement != null)
                {
                    newAnnouncement.Delete();
                }

                SPField newField = spWeb.Fields
                    .Cast<SPField>()
                    .FirstOrDefault(f => f.StaticName == "Team Project");
                if (newField != null)
                {
                    newField.Delete();
                }

                SPContentType myContentType = 
                    new SPContentType(spWeb.ContentTypes["Announcement"], 
                        spWeb.ContentTypes, "New Announcements");
                myContentType.Group = "Custom Content Types";

                spWeb.Fields.Add("Team Project", SPFieldType.Text, true);
                SPFieldLink projFeldLink = new SPFieldLink(spWeb.Fields["Team Project"]);
                myContentType.FieldLinks.Add(projFeldLink);

                SPFieldLink companyFieldLink = new SPFieldLink(spWeb.Fields["Company"]);
                myContentType.FieldLinks.Add(companyFieldLink);

                spWeb.ContentTypes.Add(myContentType);
                myContentType.Update();
            }
]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
